/**
 # # THIS FILE IS PART OF drug-server PROJECT
 # #
 # BeanUtils.java - 对象同属性拷贝工具
 # 
 # THIS PROGRAM IS OPENSOURCE SOFTWARE, IS LICENSED UNDER BSD
 # YOU SHOULD HAVE RECEIVED A COPY OF WTFPL LICENSE
 # 
 # Copyright (c) 2018 Ting Fan
 */
package com.cloudkinto.common.utils.bean;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * @program     : drug-server
 * @description : 对象同属性拷贝工具
 * @author      : Ting Fan
 * @company     : 苏州
 * @create      : 2019-09-24 09:26
 **/
public class CkBeanUtils {
    /**
     * 实现属性拷贝
     */
    public static void copyProperties(Object src , Object dest){
        try {
            //源对象的bean信息
            BeanInfo bi_src = Introspector.getBeanInfo(src.getClass());
            Class destClass = dest.getClass();

            //取出源对象类的属性描述符
            PropertyDescriptor[] ps = bi_src.getPropertyDescriptors();
            //迭代所有属性
            for(PropertyDescriptor pp : ps){
                //找出get方法和set方法
                Method getter = pp.getReadMethod();
                Method setter = pp.getWriteMethod() ;
                //标准属性
                if(getter != null && setter != null){
                    //提取getter返回值类型
                    Class retType = getter.getReturnType();
                    //获得目标对象对应的setter方法签名
                    Method destSetter = null ;
                    try {
                        destSetter = destClass.getMethod(setter.getName(), retType);

                    } catch (Exception e) {
                        continue;
                    }
                    //取出源对象的属性值
                    getter.setAccessible(true);
                    Object srcValue = getter.invoke(src) ;
                    //设置给目标对象
                    destSetter.setAccessible(true);
                    destSetter.invoke(dest,srcValue) ;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
